package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查看购物车
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 添加购物车
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        // 获取用户id，设置用户id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

         /* 查看菜品或者套餐是否点过
        如何知道是否被点过了？根据菜品id或者套餐id，查询下数据库中有没有这个数据
        */
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);

        // 判断当前用户点的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            //说明是菜品,就根据菜品id查询购物车是否有这个菜
            queryWrapper.eq(ShoppingCart::getDishId, dishId);

        } else {
            //如果为null那一定是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        }
        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if (one != null) {
            //已经存在，就在原来数量基础上加一
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        } else {
            //不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);

            //将shoppingCart的值赋值给one,方便返回数据给页面
            one = shoppingCart;
        }
        return R.success(one);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

    /**
     * 取消购物车
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        //条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        //获取当前用户id作为条件之一
        Long userId = BaseContext.getCurrentId();
        queryWrapper.eq(ShoppingCart::getUserId, userId);

        //通过判断传过来的数据是菜品还是套餐作为条件之一
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            //代表当前取消的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //代表当前取消的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //执行查询
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);

        //直接在该数据的number属性上操作,但是如果number只为1的话就直接删除该数据
        Integer number = one.getNumber();
        one.setNumber(number - 1);
        if (number - 1 > 0) {
            //说明不止一份,直接更新数据即可
            shoppingCartService.updateById(one);
        } else {
            //说明数量只有一个,取消后就没了,执行删除操作
            shoppingCartService.remove(queryWrapper);
        }

        return R.success(one);
    }

}
